package com.boot.security.server.service.impl.mffl.MffCashwithdrawalapplication;

import com.boot.security.server.dao.UserDao;
import com.boot.security.server.dao.mffl.MffCashwithdrawalapplicationDao;
import com.boot.security.server.dao.mffl.MfflBankcardDao;
import com.boot.security.server.dao.mffl.MfflConfigDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MffCashwithdrawalapplication;
import com.boot.security.server.model.mffl.MfflBankcard;
import com.boot.security.server.model.mffl.MfflConfig;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.Mfflconfig;
import com.boot.security.server.utils.mffl.ProjectUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 提现业务
 */
@Service
public class MffCashwithdrawalapplicationService {
    //提现
    @Autowired
    private MffCashwithdrawalapplicationDao mffCashwithdrawalapplicationDao;

    //用户表
    @Autowired
    MfflUserDao mffluserdao;

    //系统表
    @Autowired
    UserDao userdao;

    //银行卡
    @Autowired
    private MfflBankcardDao mfflBankcardDao;

    //系统配置表
    @Autowired
    private MfflConfigDao mfflConfigDao;

    /**
     * 获取账户全部金额
     *
     * @return
     */
    public ReturnBean getUserMoneyAll() {
        //获取当前用户
        SysUser sysUser = UserUtil.getLoginUser();
        MfflUser mffluser = mffluserdao.getByUserId(sysUser.getId());
        ReturnBean returnBean = new ReturnBean();
        BigDecimal subtract = ProjectUtil.subtract(mffluser.getMoney(), mffluser.getFrozenmoney());
        returnBean.setDatas(subtract.toString());
        return returnBean;
    }

    /**
     * 提现申请
     *
     * @param money
     * @param bankcardnumber
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnBean applyCashWithdrawal(Double money, String bankcardnumber) throws Exception {
        ReturnBean returnBean = new ReturnBean();
        try {
            SysUser sysUser = UserUtil.getLoginUser();
            //用户附属
            MfflUser mffluser = mffluserdao.getByUserId(sysUser.getId());
            //银行卡
            MfflBankcard UserIdBankcard = mfflBankcardDao.getByUserId(sysUser.getId().intValue());

            //平台扣点记录
            MfflConfig byName = mfflConfigDao.getByName(Mfflconfig.points.name());
            double points = Double.parseDouble(byName.getValue());
            Double cashWithdrawalmoney = ProjectUtil.Points(money, points);

            if (mffluser.getLawyerInspect() != 1) {
                return new ReturnBean("201", "当前用户审核状态，不允许提现操作");
            }

            if (UserIdBankcard == null) {
                return new ReturnBean("201", "当前用户未设置银行卡号");
            }
            if (!UserIdBankcard.getBankcardnumber().equals(bankcardnumber)) {
                return new ReturnBean("201", "当前银行卡号与申请提现银行卡号不一致");
            }
            if (cashWithdrawalmoney > (mffluser.getMoney() - mffluser.getFrozenmoney())) {
                return new ReturnBean("201", "提现金额不允许超出账户金额");
            }

            try {
                MffCashwithdrawalapplication mcsdac = new MffCashwithdrawalapplication();
                mcsdac.setUserid(sysUser.getId().intValue());
                mcsdac.setNowmoney(mffluser.getMoney());//当前现金/元
                mcsdac.setExpenditure(money);           //提现金额/元
                mcsdac.setPoints(String.valueOf(points));//扣点比例
                mcsdac.setPointsmoney(cashWithdrawalmoney);//扣点后金额
                mcsdac.setSurplus(mffluser.getMoney() - cashWithdrawalmoney);//剩余金额/元
                mcsdac.setState(3);
                mcsdac.setReasonsforfailure("");
                mcsdac.setOperator(0);
                Date date = new Date();
                mcsdac.setCreateTime(date);
                mcsdac.setUpdateTime(date);
                mcsdac.setDeleteTime(date);
                mcsdac.setDeleteState(1);
                mffCashwithdrawalapplicationDao.save(mcsdac);

                //冻结用户金额
                MfflUser frozenmoney = new MfflUser();
                frozenmoney.setId(mffluser.getId());
                frozenmoney.setFrozenmoney(ProjectUtil.addition(money,mffluser.getFrozenmoney()));
                mffluserdao.update(frozenmoney);
                return returnBean;
            } catch (Exception e) {
                e.printStackTrace();
                returnBean.setReturnBean("105");
                return returnBean;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("提现异常");
        }
    }

    /**
     * 按id查询
     *
     * @param id
     * @return
     */
    public ReturnBean<MffCashwithdrawalapplication> lookWithdrawalRecord(long id) {
        //当前用户
        SysUser sysUser = UserUtil.getLoginUser();
        ReturnBean returnBean = new ReturnBean();
        MffCashwithdrawalapplication byId = mffCashwithdrawalapplicationDao.getByuser(id, sysUser.getId().intValue());
        if (byId == null) {
            return new ReturnBean("201", "没有此id提现记录");
        }
        returnBean.setDatas(byId);
        return returnBean;
    }

    /**
     * 取消申请提现
     *
     * @param id
     * @return
     */
    @Transactional
    public ReturnBean cancel(Long id) {
        ReturnBean returnBean = new ReturnBean();
        SysUser sysUser = UserUtil.getLoginUser();
        MffCashwithdrawalapplication byId = mffCashwithdrawalapplicationDao.getByuser(id, sysUser.getId().intValue());
        if (byId == null) {
            return new ReturnBean("201", "没有此id提现记录");
        }
        if (byId.getState() != 3) {
            return new ReturnBean("201", "当前提现记录状态不允许取消");
        }

        try {
            //修改状态
            MffCashwithdrawalapplication mcsdac = new MffCashwithdrawalapplication();
            mcsdac.setId(byId.getId());
            mcsdac.setState(4);
            Date date = new Date();
            mcsdac.setUpdateTime(date);
            mffCashwithdrawalapplicationDao.update(mcsdac);

            //用户附属
            MfflUser mffluser = mffluserdao.getByUserId(sysUser.getId());
            //冻结用户金额
            MfflUser frozenmoney = new MfflUser();
            frozenmoney.setId(mffluser.getId());
            frozenmoney.setFrozenmoney(ProjectUtil.subtract(mffluser.getFrozenmoney(),byId.getExpenditure()));
            frozenmoney.setUpdateTime(date);
            mffluserdao.update(frozenmoney);
            return returnBean;
        } catch (Exception e) {
            e.printStackTrace();
            returnBean.setReturnBean("105");
            return returnBean;
        }
    }
}
